package com.zx.mes.hyl.auth.rest;

import com.zx.mes.hyl.auth.pagemodel.User;
import com.zx.mes.hyl.auth.service.RoleServiceI;
import com.zx.mes.hyl.auth.service.UserServiceI;
import com.zx.mes.hyl.common.msg.ObjectRestResponse;
import com.zx.mes.hyl.common.pagemodel.Json;
import com.zx.mes.hyl.common.pagemodel.JsonResult;
import com.zx.mes.hyl.common.pagemodel.LayPageHelper;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

/**
 * 用户控制器
 * 
 * @author 
 * 
 */
@Controller
@RequestMapping("userController")
public class UserController {

	private static final Logger logger=Logger.getLogger(UserController.class);

	@Autowired
	private UserServiceI userService;

	@Autowired
	private RoleServiceI roleService;


	/**
	 * 用户注册
	 * 
	 * @param user
	 *            用户对象
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/reg")
	public Json reg(User user) {
		Json j = new Json();
		try {
			userService.reg(user);
			j.setSuccess(true);
			j.setMsg("注册成功！新注册的用户没有任何权限，请让管理员赋予权限后再使用本系统！");
			j.setObj(user);
			j.setStatus(200);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

	/**
	 * 跳转到用户管理页面
	 *
	 * @return
	 */
	@RequestMapping("/manager")
	public String manager() {
		return "html/user/user-list";
	}



	@ResponseBody
	@RequestMapping("/list")
	public JsonResult<List> list(User user, LayPageHelper ph ){
		JsonResult<List> jsonResult=new JsonResult<>();
		List<User> userList=userService.findAll(user,ph);

		jsonResult.setIS_SUCCESS(true);
		jsonResult.setCode(0);
		jsonResult.setStatus(200);
		jsonResult.setData(userList);
		jsonResult.setCount((int) userService.count(user));
		jsonResult.setMsg("");
		return jsonResult;
	}


	@RequestMapping("/getOne")
	@ResponseBody
	public ObjectRestResponse<List> getOne(String id){
		ObjectRestResponse<List> response=new ObjectRestResponse<>();
		response.setData(userService.get(id).getRoleList());
		return response;
	}


	/**
	 * 跳转到添加用户页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/addPage")
	public String addPage(HttpServletRequest request,Model model) {
		User u = new User();
		u.setId(UUID.randomUUID().toString());
		model.addAttribute("user",u);
		return "html/user/user-add";
	}

	/**
	 * 添加用户
	 * 
	 * @return
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Json add(User user) {
		Json j = new Json();
		try {
			userService.add(user);
			j.setSuccess(true);
			j.setMsg("添加成功！");
			j.setObj(user);
			j.setStatus(200);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

	/**
	 * 跳转到用户修改页面
	 * 
	 * @return
	 */
	@RequestMapping("/editPage")
	public String editPage() {
		return "html/user/user-edit";
	}

	/**
	 * 修改用户
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(User user) {
		Json j = new Json();
		try {
			userService.edit(user);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
			j.setObj(user);
			j.setStatus(200);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

	/**
	 * 删除用户
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Json delete(String id, HttpSession session) {
		Json j = new Json();
		if (id != null && !id.equalsIgnoreCase("")) {
			userService.delete(id);
		}
		j.setMsg("删除成功！");
		j.setSuccess(true);
		j.setStatus(200);
		return j;
	}

	/**
	 * 批量删除用户
	 * 
	 * @param ids
	 *            ('0','1','2')
	 * @return
	 */
	@RequestMapping("/batchDelete")
	@ResponseBody
	public Json batchDelete(String ids, HttpSession session) {
		Json j = new Json();
		if (ids != null && ids.length() > 0) {
			for (String id : ids.split(",")) {
				if (id != null) {
					this.delete(id, session);
				}
			}
		}
		j.setMsg("批量删除成功！");
		j.setSuccess(true);
		j.setStatus(200);
		return j;
	}

	/**
	 * 跳转到用户授权页面
	 * 
	 * @return
	 */
	@RequestMapping("/grantPage")
	public String grantPage() {

		return "html/user/grant-list";
	}

	/**
	 * 用户授权
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping("/grant")
	@ResponseBody
	public Json grant(User user) {
		Json j = new Json();
		userService.grant(user.getIds(), user);
		j.setSuccess(true);
		j.setMsg("授权成功！");
		j.setStatus(200);
		return j;
	}

	/**
	 * 跳转到编辑用户密码页面
	 * 
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/editPwdPage")
	public String editPwdPage(String id, HttpServletRequest request) {
		User u = userService.get(id);
		request.setAttribute("user", u);
		return "html/admin/userEditPwd";
	}

	/**
	 * 跳转到编辑自己的密码页面
	 * 
	 * @return
	 */
	@RequestMapping("/editCurrentUserPwdPage")
	public String editCurrentUserPwdPage() {
		return "html/user/userEditPwd";
	}

	/**
	 * 跳转到显示用户权限页面
	 * 
	 * @return
	 */
	@RequestMapping("/currentUserResourcePage")
	public String currentUserResourcePage(HttpServletRequest request, HttpSession session) {
		return "html/user/userResource";
	}
}
